using global::Nacos.V2.DependencyInjection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Net;
using Yarp.ReverseProxy.Configuration;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(options =>
{
#if DEBUG == false
    options.Listen(IPAddress.Any, 81, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http2;
    });

    options.Listen(IPAddress.Any, 80, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1;
    });
#endif
});


builder.Services.AddNacosV2Naming(builder.Configuration);

//////builder.Services.AddNacosV2Naming(x =>
//////{
//////    x.ServerAddresses = new System.Collections.Generic.List<string> { "http://47.114.59.171:8848/" };
//////    x.Namespace = "v3local";
//////    x.UserName = "v3local";
//////    x.Password = "Aa000000";
//////    x.NamingUseRpc = true;
//////});

builder.Services.AddReverseProxy()
    .AddNacosServiceDiscovery(percount: 100, autoRefreshPeriod: 30);

var app = builder.Build();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/yarp", (IProxyConfigProvider provider) =>
    {
        var res = provider.GetConfig();
        return Results.Ok(res);
    });
    endpoints.MapReverseProxy();
});

app.Run();
